// 创建uploads目录（如果不存在）
const fs = require("fs");
const multer = require("multer");
const uploadDir = "uploads";
if (!fs.existsSync(uploadDir)) {
  fs.mkdirSync(uploadDir);
}

// 配置multer使用原始文件名存储
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, uploadDir);
  },
  filename: function (req, file, cb) {
    // 使用原始文件名，这样会自动覆盖同名文件
    cb(null, file.originalname);
  },
});

const upload = multer({ storage: storage });

module.exports = upload;